package SA.teacher;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Calendar;

import SA.teacher.daily.DailyActivity1;
import SA.teacher.manage.ManageActivity1;
import SA.teacher.R;
import SA.teacher.R.drawable;
import SA.teacher.R.id;
import SA.teacher.R.layout;
import android.app.*;
import android.content.*;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class MainActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		

		parse ps = new parse();
		ps.run();
		
		

		Calendar today = Calendar.getInstance();
		TextView todayDate = (TextView) findViewById(R.id.today);
		todayDate.setText((today.get(Calendar.MONTH) + 1) + "월"+ today.get(Calendar.DATE) + "일");

		final String[] classes = new String[] { "1 - A", "1 - B", "2 - A" };

		ImageView manageBtn = (ImageView) findViewById(R.id.manageBtn);
		manageBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				new AlertDialog.Builder(MainActivity.this)
				.setTitle("반을 선택해주세요.")
				.setItems(classes,
						new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog,	int which) {
						Intent intent = new Intent(MainActivity.this, ManageActivity1.class);
						intent.putExtra("classname",classes[which]);
						startActivity(intent);
					}
				}).show();
			}
		});
		
		
		ImageView dailyBtn = (ImageView) findViewById(R.id.dailyBtn);
		dailyBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				dailyActivity();
			}
		});
		
		
		
		
	}
	

public class parse implements Runnable {
	public void run() {
		
		// TODO Auto-generated method stub
		try {
			//String addr ="Http://220.67.128.8/temp.php?useUnicode=true&characterEncoding=EUC-KR&tID=1234" + "&adate=20120902" +  "&classid="+  classid + "&content="+content;
			
			
			URL url = new URL("Http://220.67.128.8/noticeview.php");
			
			//Log.e("1", tid);
			//Log.e("2", adate);
			//Log.e("3", classid);
			//url.openStream();
			//URL url = new URL(addr);
			
			HttpURLConnection conn = (HttpURLConnection) url
					.openConnection();
			
			InputStream  is   = null;
			ByteArrayOutputStream baos = null; 
			Log.e("login 응답 : ", "접속0");
			if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
				Log.e("login 응답 : ", "접속1");
				is = conn.getInputStream();  
				Log.e("login 응답 : ", "접속2");
				baos = new ByteArrayOutputStream();
				Log.e("login 응답 : ", "접속3");
				byte[] byteBuffer = new byte[1024];    byte[] byteData = null; 
				int nLength = 0;
				Log.e("login 응답 : ", "접속4");
				while((nLength = is.read(byteBuffer)) > 0) {   
					baos.write(byteBuffer, 0, nLength); 
				}
				Log.e("login 응답 : ", "접속5");
				byteData = baos.toByteArray(); 
				Log.e("login 응답 : ", "접속6");
				String response = new String(byteData, "euc-kr");   
				//String response = new String(byteData);  
				Log.e("login 응답 : ", "접속7");
				Log.e("login 응답 : ", response);
				if(response==null || response.equals("")) {    
					throw new Exception("Response is NULL!");  
				}
				TextView notice = (TextView) findViewById(R.id.notice);
				notice.setGravity(Gravity.CENTER);
				Log.e("login 응답 : ", response);
		        notice.setText(response);
		        
			}
			else{
				Log.e("login 응답 : ", "접속 안됨");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
		}
	}
}

	

	void manageActivity() {
		Intent intent = new Intent(this, ManageActivity1.class);
		startActivity(intent);
	}
	
	void dailyActivity() {
		Intent intent = new Intent(this, DailyActivity1.class);
		startActivity(intent);
	}

	
	
	
}
